/**
 * 授权 API
 */
import {
	isWechat
} from '@/common/util.js';
import {
	http
} from '../request/main.js';

/**
 * 用户登录 - 手机验证码
 * 
 * @returns {Promise}
 */
export function loginByPhoneCaptcha(phone, captcha, wx_code, auth_key) {
	return http.post('/user/loginByPhoneCaptcha', {
		phone,
		captcha,
		wx_code,
		auth_key
	}, {
		muteLogin: true
	})
}

/**
 * 用户登录 - 微信快捷登录
 * 
 * @returns {Promise}
 */
export function loginByWechatPhone(encrypted_data, iv, code, wx_code) {
	return http.post('/user/loginByWechatPhone', {
		encrypted_data,
		iv,
		code,
		wx_code
	}, {
		muteLogin: true
	})
}

/**
 * 小程序自动登录
 * 
 * @returns {Promise}
 */
export function wechatAutoLogin(wx_code) {
	return http.post('/user/wechatAutoLogin', {
		wx_code
	}, {
		muteLogin: true
	})
}

/**
 * 公众号自动登录
 * 
 * @returns {Promise}
 */
export function mpAutoLogin(mp_code) {
	return http.post('/user/mpAutoLogin', {
		mp_code
	}, {
		muteLogin: true
	})
}


/**
 * 发送手机验证吗
 * 
 * @returns {Promise}
 */
export function sendPhoneCaptcha(phone, wx_code, auth_key) {
	return http.post('/user/sendPhoneCaptcha', {
		phone,
		wx_code,
		auth_key
	}, {
		muteLogin: true
	})
}


/**
 * 用户登出
 * 
 * @returns {Promise}
 */
export function logout() {
	let source = null
	// #ifdef  MP-WEIXIN
	source = 'mini'
	// #endif
	// #ifdef  H5
	if (isWechat()) {
		source = 'mp'
	}
	// #endif
	return http.post('/user/logout', {
		source
	})
}

/**
 * 更新用户信息
 * 
 * @returns {Promise}
 */
export function updateUserInfo(avatar_url, gender) {
	let data = {}
	if (avatar_url) {
		data.avatar_url = avatar_url
	}
	if (gender !== undefined) {
		data.gender = gender
	}
	return http.post('/user/updateUserInfo', data)
}

/**
 * 获取用户信息
 * 
 * @returns {Promise}
 */
export function getUserInfo() {
	return http.post('/user/getUserInfo')
}

/**
 * 修改手机号
 * 
 * @returns {Promise}
 */
export function changePhone(phone, captcha) {
	return http.post('/user/changePhone', {
		phone,
		captcha
	})
}

/**
 * 修改手机号 - 授权微信手机号
 * 
 * @returns {Promise}
 */
export function changeWechatPhone(encrypted_data, iv, code, wx_code) {
	return http.post('/user/changeWechatPhone', {
		encrypted_data,
		iv,
		code,
		wx_code
	})
}

/**
 * 修改昵称
 * 
 * @returns {Promise}
 */
export function changeNickname(nickname) {
	return http.post('/user/changeNickname', {
		nickname
	})
}

/**
 * 保存订阅
 * 
 * @returns {Promise}
 */
export function saveSubscribe(template_ids) {
	return http.post('/user/saveSubscribe', {
		template_ids
	})
}
